/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import crm.salesOrderProcessing.entity.LineItemEntity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import mrp.productionPlanning.entity.BackorderEntity;
import mrp.productionPlanning.entity.DailyDetailEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class processSalesOrderSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
    @EJB
    private DailyDetailSessionBean ddsb = new DailyDetailSessionBean();
    @EJB
    private InventoryControlSessionBean icsb = new InventoryControlSessionBean();
    @EJB
    BackorderSessionBean bosb = new BackorderSessionBean();
    
    public int leadTimeEstimation (List<LineItemEntity> lineItemList){
        int count  = 0;
        Calendar cal = Calendar.getInstance();
        int qA =0;
        int qB =0;
        int qC =0;
        int qD =0;
        
        System.out.println("***************Start " + cal.getTime().toString());
        int correction = cal.get(Calendar.MONTH);
        
        
        int quantity = 0;
        for (int i=0;i<lineItemList.size();i++){
            quantity = lineItemList.get(i).getQuantity();
            if (lineItemList.get(i).getProduct().getId().equals(1L)){
                qA += quantity;
            }
            else if (lineItemList.get(i).getProduct().getId().equals(2L)){
                qB += quantity;
            }
            else if (lineItemList.get(i).getProduct().getId().equals(3L)){
                qC += quantity;
            }
            else if (lineItemList.get(i).getProduct().getId().equals(4L)){
                qD += quantity;
            }
        }
        
        System.out.println("qA : "+ qA);
        System.out.println("qB : "+ qB);
        System.out.println("qC : "+ qC);
        System.out.println("qD : "+ qD);
        
        qA -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Nuts");
        qB -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Fruits");
        qC -= icsb.getCurrentInventoryLevel("Premium Power Bar-Chocolate Nuts");
        qD -= icsb.getCurrentInventoryLevel("Premium Power Bar-Redcurrant Fruits");
        if (qA <0) {qA = 0;}
        if (qB <0) {qB = 0;}
        if (qC <0) {qC = 0;}
        if (qD <0) {qD = 0;}        
        
        System.out.println("qA 2nd: "+ qA);
        System.out.println("qB 2nd: "+ qB);
        System.out.println("qC 2nd: "+ qC);
        System.out.println("qD 2nd: "+ qD);
        
        float jA, jB, jC, jD;
        
        jA = qA;
        jB = qB;
        jC = qC;
        jD = qD;
        
        float time = jA/50 + jB/50 + jC/45 + jD/45;
        
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
//        String temp = sdf.format(cal.getTime());
//        Date dtemp = new Date();
////        String temp = "05/12/2012";
//        try{
//            dtemp = sdf.parse(temp);
//        }
//        catch (ParseException ex){}
//            dtemp.setMinutes(1);
//            cal.setTime(dtemp);
//            cal.set(Calendar.MONTH, correction);
//            dtemp = cal.getTime();
            System.out.println("^^^^^^^^^"+cal.getTime().toString());
            System.out.println("Starting required time: "+ time);
            
            while (time > (ddsb.getDailyDetailByDate(cal.getTime()).getLeftCapacity())+ ddsb.getDailyDetailByDate(cal.getTime()).getLeftOvertime()){
                
                    
                    float tempA = qA;
                    float tempB = qD;
                    float tempC = qC;
                    float tempD = qD;
                    DailyDetailEntity day = ddsb.getDailyDetailByDate(cal.getTime());
                    qA = qA - day.getScheduleA() + day.getReservedA();
                    qB = qB - day.getScheduleB() + day.getReservedB();
                    qC = qC - day.getScheduleC() + day.getReservedC();
                    qD = qD - day.getScheduleD() + day.getReservedD();
                    System.out.println();
                    float tA = (day.getScheduleA()-day.getReservedA());
                    tA = tA/50;
                    float tB = (day.getScheduleB()-day.getReservedB());
                    tB = tB/50;
                    float tC = (day.getScheduleC()-day.getReservedC());
                    tC = tC/50;
                    float tD = (day.getScheduleD()-day.getReservedD());
                    tD = tD/50;
                    tempA = tempA/50;
                    tempB = tempB/50;
                    tempC = tempC/50;
                    tempD = tempD/50;
                    float fA = 0;
                    float fB = 0;
                    float fC = 0;
                    float fD = 0;
                    fA = getSmaller(tempA, tA);
                    fB = getSmaller(tempB, tB);
                    fC = getSmaller(tempC, tC);
                    fD = getSmaller(tempD, tD);
                    if (qA <0) {qA = 0;}
                    if (qB <0) {qB = 0;}
                    if (qC <0) {qC = 0;}
                    if (qD <0) {qD = 0;}
                    System.out.println("Date: "+ cal.getTime().toString());
                    System.out.println("Quantity of A: "+ qA);
                    System.out.println("Quantity of B: "+ qB);
                    System.out.println("Quantity of C: "+ qC);
                    System.out.println("Quantity of D: "+ qD);
                    System.out.println("Reduced A: " + fA);
                    System.out.println("Reduced B: " + fB);
                    System.out.println("Reduced C: " + fC);
                    System.out.println("Reduced D: " + fD);
                    System.out.println("Reduced Time A: " + tA);
                    System.out.println("Reduced Time B: " + tB);
                    System.out.println("Reduced Time C: " + tC);
                    System.out.println("Reduced Time D: " + tD);
                    time = time - fA - fB - fC - fD;
                    System.out.println(time);
                    time -= day.getLeftCapacity();
                    float amount = day.getLeftCapacity();
                    tempA = qA;
                    tempB = qB;
                    tempC = qC;
                    tempD = qD;
                    //***********************************************************************************************************
                    if (qA > 0 ){
                        if (tempA/50 > day.getLeftCapacity()){
                            qA -= (int)(day.getLeftCapacity()*50);
                        }
                        else {
                            amount = day.getLeftCapacity() - tempA/50;
                            qA = 0;
                            if (tempB/50 > amount){
                                qB -= (int)amount*50;
                            }
                            else{
                                qB = 0;
                                amount -= tempB/50;
                                if (tempC > amount){
                                    qC -= (int)amount*45;
                                }
                                else{
                                    qC = 0;
                                    amount -= tempC/50;
                                    if (tempD > amount){
                                        qD -= (int)amount*45;
                                    }
                                    else{
                                        amount -= tempD/45;
                                        qD =0;
                                    }
                                }
                            }
                        }
                    }
                    else {
                        if (qB >0){
                            amount = day.getLeftCapacity();
                            if (tempB/50 > amount){
                                qB -= (int)amount*50;
                            }
                            else{
                                qB = 0;
                                amount -= tempB/50;
                                if (tempC > amount){
                                    qC -= (int)amount*45;
                                }
                                else{
                                    qC = 0;
                                    amount -= tempC/50;
                                    if (tempD > amount){
                                        qD -= (int)amount*45;
                                    }
                                    else{
                                        amount -= tempD/45;
                                        qD =0;
                                    }
                                }
                            }
                        }
                        else {
                            if (qC>0){
                                amount = day.getLeftCapacity();
                                if (tempC > amount){
                                    qC -= (int)amount*45;
                                }
                                else{
                                    qC = 0;
                                    amount -= tempC/50;
                                    if (tempD > amount){
                                        qD -= (int)amount*45;
                                    }
                                    else{
                                        amount -= tempD/45;
                                        qD =0;
                                    }
                                }
                            }
                            else {
                                amount = day.getLeftCapacity();
                                qD -= (int)amount*45;
                            }
                        }
                    }
                    
                    
                    System.out.println(time);
                    time -= day.getLeftOvertime();
                    //***********************************************************************************************************
                    amount = day.getLeftOvertime();
                    
                    if (qA > 0 ){
                        if (tempA/50 > day.getLeftOvertime()){
                            qA -= (int)(day.getLeftOvertime()*50);
                        }
                        else {
                            amount = day.getLeftOvertime() - tempA/50;
                            qA = 0;
                            if (tempB/50 > amount){
                                qB -= (int)amount*50;
                            }
                            else{
                                qB = 0;
                                amount -= tempB/50;
                                if (tempC > amount){
                                    qC -= (int)amount*45;
                                }
                                else{
                                    qC = 0;
                                    amount -= tempC/50;
                                    if (tempD > amount){
                                        qD -= (int)amount*45;
                                    }
                                    else{
                                        amount -= tempD/45;
                                        qD =0;
                                    }
                                }
                            }
                        }
                    }
                    else {
                        if (qB >0){
                            amount = day.getLeftOvertime();
                            if (tempB/50 > amount){
                                qB -= (int)amount*50;
                            }
                            else{
                                qB = 0;
                                amount -= tempB/50;
                                if (tempC > amount){
                                    qC -= (int)amount*45;
                                }
                                else{
                                    qC = 0;
                                    amount -= tempC/50;
                                    if (tempD > amount){
                                        qD -= (int)amount*45;
                                    }
                                    else{
                                        amount -= tempD/45;
                                        qD =0;
                                    }
                                }
                            }
                        }
                        else {
                            if (qC>0){
                                amount = day.getLeftOvertime();
                                if (tempC > amount){
                                    qC -= (int)amount*45;
                                }
                                else{
                                    qC = 0;
                                    amount -= tempC/50;
                                    if (tempD > amount){
                                        qD -= (int)amount*45;
                                    }
                                    else{
                                        amount -= tempD/45;
                                        qD =0;
                                    }
                                }
                            }
                            else {
                                amount = day.getLeftOvertime();
                                qD -= (int)amount*45;
                            }
                        }
                    }
                    //***********************************************************************************************************
                    System.out.println("Left Time: " + time);
                    if (time>0){
                        count ++;
                    }
                    else {time =0;}
                    cal.add(Calendar.DATE, 1);
                while (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7 ){
                    cal.add(Calendar.DATE, 1);
                }
                while (ddsb.getDailyDetailByDate(cal.getTime()) == null){
                    cal.add(Calendar.DATE, 1);
                }
                System.out.println("*********************************************************************");
            }
        count += 2;

        System.out.println("The estimated lead time: "+ count);
        return count;
    }
    
    public int leadTimeEstimation (int demandA, int demandB, int demandC, int demandD){
        int count  = 0;
        Calendar cal = new GregorianCalendar();
        int qA =demandA;
        int qB =demandB;
        int qC =demandC;
        int qD =demandD;
        
        qA -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Nuts");
        qB -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Fruits");
        qC -= icsb.getCurrentInventoryLevel("Premium Power Bar-Chocolate Nuts");
        qD -= icsb.getCurrentInventoryLevel("Premium Power Bar-Redcurrant Fruits");
        if (qA <0) {qA = 0;}
        if (qB <0) {qB = 0;}
        if (qC <0) {qC = 0;}
        if (qD <0) {qD = 0;}        
        
        float time = qA/50 + qB/50 + qC/45 + qD/45;
        
        while (time > (ddsb.getDailyDetailByDate(cal.getTime()).getLeftCapacity())+ ddsb.getDailyDetailByDate(cal.getTime()).getLeftOvertime()){
            DailyDetailEntity day = ddsb.getDailyDetailByDate(cal.getTime());
            qA = qA - day.getScheduleA() + day.getReservedA();
            qB = qB - day.getScheduleB() + day.getReservedB();
            qC = qC - day.getScheduleC() + day.getReservedC();
            qD = qD - day.getScheduleD() + day.getReservedD();
            if (qA <0) {qA = 0;}
            if (qB <0) {qB = 0;}
            if (qC <0) {qC = 0;}
            if (qD <0) {qD = 0;}
            time = qA/50 + qB/50 + qC/45 + qD/45;
            time -= day.getLeftCapacity();
            time -= day.getLeftOvertime();
            count ++;
            cal.add(Calendar.DATE, 1);
        }
        
        return count;
    }
    
    public int leadTimeEstimation (DeliveryOrderEntity doe){
        
//        int count = 0;
//        Calendar cal = new GregorianCalendar();
//        float time = 0;
//        List<LineItemEntity> lineItems = doe.getLineItems();
//        
//        for (LineItemEntity lineItem: lineItems){
//            String name = lineItem.getProduct().getProductName();
//            if(name.equalsIgnoreCase("productA") || name.equalsIgnoreCase("productB")){
//                time += lineItem.getQuantity()/50;
//            }
//            else{
//                time += lineItem.getQuantity()/45;
//            }       
//        }
//        
//        while (availableCapacityUntil(cal.getTime())<time){
//            count ++;
//            cal.add(Calendar.DATE, 1);
//        }
//        count ++;
//        
//        return count;
        int count  = 0;
        Calendar cal = new GregorianCalendar();
        int qA =0;
        int qB =0;
        int qC =0;
        int qD =0;
        List<LineItemEntity> lineItems = doe.getLineItems();
        
        for (LineItemEntity lineItem : lineItems){
            if (lineItem.getProduct().getProductName().equalsIgnoreCase("productA")){
                qA += lineItem.getQuantity();
            }
            else if (lineItem.getProduct().getProductName().equalsIgnoreCase("productB")){
                qB += lineItem.getQuantity();
            }
            else if (lineItem.getProduct().getProductName().equalsIgnoreCase("productC")){
                qC += lineItem.getQuantity();
            }
            else if (lineItem.getProduct().getProductName().equalsIgnoreCase("productD")){
                qD += lineItem.getQuantity();
            }
        }
        qA -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Nuts");
        qB -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Fruits");
        qC -= icsb.getCurrentInventoryLevel("Premium Power Bar-Chocolate Nuts");
        qD -= icsb.getCurrentInventoryLevel("Premium Power Bar-Redcurrant Fruits");
        if (qA <0) {qA = 0;}
        if (qB <0) {qB = 0;}
        if (qC <0) {qC = 0;}
        if (qD <0) {qD = 0;}        
        
        float time = qA/50 + qB/50 + qC/45 + qD/45;

        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
        String temp = sdf.format(cal.getTime());
        Date dtemp = new Date();
//        String temp = "05/12/2012";
        try{
            dtemp = sdf.parse(temp);
        }
        catch(ParseException ex){
        System.out.println("parse failed");
        }
            System.out.println("^^^^^^^^^"+dtemp.toString());
            cal.setTime(dtemp);
            cal.set(Calendar.MINUTE, 1);
        while (time > (ddsb.getDailyDetailByDate(cal.getTime()).getLeftCapacity())+ ddsb.getDailyDetailByDate(cal.getTime()).getLeftOvertime()){
            DailyDetailEntity day = ddsb.getDailyDetailByDate(cal.getTime());
            qA = qA - day.getScheduleA() + day.getReservedA();
            qB = qB - day.getScheduleB() + day.getReservedB();
            qC = qC - day.getScheduleC() + day.getReservedC();
            qD = qD - day.getScheduleD() + day.getReservedD();
            if (qA <0) {qA = 0;}
            if (qB <0) {qB = 0;}
            if (qC <0) {qC = 0;}
            if (qD <0) {qD = 0;}
            time = qA/50 + qB/50 + qC/45 + qD/45;
            time -= day.getLeftCapacity();
            time -= day.getLeftOvertime();
            count ++;
            cal.add(Calendar.DATE, 1);
        }
//        }
//        catch (ParseException e){
//            System.out.println("parse failed");
//        }
//        
        return count;
    }
    
    public float availableCapacityUntil(Date endDate){
        
        float capacity = 0;
        
        Calendar cal = new GregorianCalendar();
        
        Date current = cal.getTime();
        cal.setTime(current);
        
        while (cal.getInstance().before(endDate)){
            capacity += ddsb.getDailyDetailByDate(cal.getTime()).getLeftCapacity();
            capacity += ddsb.getDailyDetailByDate(cal.getTime()).getLeftOvertime();
            cal.add(Calendar.DATE, 1);
        }
        return capacity;
        
    }
    
    public boolean needBackorder(DeliveryOrderEntity doe){
        
        boolean out = true;
        Date dueDate = null;
        Calendar cal = new GregorianCalendar();
        float time = 0;
        int quantityA = 0;
        int quantityB = 0;
        int quantityC = 0;
        int quantityD = 0;
        List<LineItemEntity> lineitems = doe.getLineItems();
        for (LineItemEntity lineitem:lineitems){
            String name = lineitem.getProduct().getProductName();
            if (name.equalsIgnoreCase("Classic Nutri Bar-Nuts")){
                quantityA += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("Classic Nutri Bar-Fruits")){
                quantityB += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("Premium Power Bar-Chocolate Nuts")){
                quantityC += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("Premium Power Bar-Redcurrant Fruits")){
                quantityD += lineitem.getQuantity();
            }
        }
        
        //********************************************************************************************************
        
        
        List<DailyDetailEntity> period = ddsb.getPeriod(Calendar.getInstance().getTime(), doe.getDeliveryDate());
        int plannedA = 0;
        int plannedB = 0;
        int plannedC = 0;
        int plannedD = 0;
        for(DailyDetailEntity day : period){
            plannedA += day.getScheduleA();
            plannedA -= day.getReservedA();
            plannedB += day.getScheduleB();
            plannedB -= day.getReservedB();
            plannedC += day.getScheduleC();
            plannedC -= day.getReservedC();
            plannedD += day.getScheduleD();
            plannedD -= day.getReservedD();
        }
        out = (quantityA > plannedA || quantityB > plannedB || quantityC > plannedC || quantityD > plannedD);
        
//        BackorderEntity boe = new BackorderEntity();
//        
//        if(out){
//            int demandA = quantityA - plannedA;
//            int demandB = quantityB - plannedB;
//            int demandC = quantityC - plannedC;
//            int demandD = quantityD - plannedD;
//            if (demandA < 0){
//                demandA = 0;
//            }
//            if (demandB < 0){
//                demandB = 0;
//            }
//            if (demandC < 0){
//                demandC = 0;
//            }
//            if (demandD < 0){
//                demandD = 0;
//            }
//            boe.setDemandA(demandA);
//            boe.setDemandB(demandB);
//            boe.setDemandC(demandC);
//            boe.setDemandD(demandD);
//            boe.setDueDate(boe.getDueDate());
//            boe.setDeliveryOrder(doe);
//            bosb.addBackorder(boe);
//        }        
        
        return out;
    }
    
    
    public void scheduleOrder(DeliveryOrderEntity doe){
        Calendar cal = new GregorianCalendar();
        Date dueDate = doe.getDeliveryDate();
        List<DailyDetailEntity> period = ddsb.getPeriod(cal.getTime(), dueDate);
        int quantityA = 0;
        int quantityB = 0;
        int quantityC = 0;
        int quantityD = 0;
        List<LineItemEntity> lineitems = doe.getLineItems();
        for (LineItemEntity lineitem:lineitems){
            String name = lineitem.getProduct().getProductName();
            if (name.equalsIgnoreCase("Classic Nutri Bar-Nuts")){
                quantityA += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("Classic Nutri Bar-Fruits")){
                quantityB += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("Premium Power Bar-Chocolate Nuts")){
                quantityC += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("Premium Power Bar-Redcurrant Fruits")){
                quantityD += lineitem.getQuantity();
            }
        }
        System.out.println("xhx TestA....." + quantityA);
        System.out.println("xhx TestB....." + quantityB);
        System.out.println("xhx TestC....." + quantityC);
        System.out.println("xhx TestD....." + quantityD);
        //******************************************Schedule for ProductA Started***************************************************************
        for (DailyDetailEntity day:period){
            if (quantityA < (day.getScheduleA() - day.getReservedA())){
                day.setReservedA(day.getReservedA() + quantityA);
                quantityA = 0;
                ddsb.updateDailyDetail(day);
            }
            else {
                quantityA -= (day.getScheduleA() - day.getReservedA());
                day.setReservedA(day.getScheduleA());
                ddsb.updateDailyDetail(day);
            }
        }
        if (quantityA > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftCapacity()*50);
                if (quantityA < capacity){
                    day.setScheduleA(day.getScheduleA()+quantityA);
                    day.setReservedA(day.getScheduleA());
                    quantityA = 0;
                }
                else {
                    quantityA -= capacity;
                    day.setScheduleA(day.getScheduleA()+capacity);
                    day.setReservedA(day.getScheduleA());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        if (quantityA > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftOvertime()*50);
                if (quantityA < capacity){
                    day.setScheduleA(day.getScheduleA()+quantityA);
                    day.setReservedA(day.getScheduleA());
                    quantityA = 0;
                }
                else {
                    quantityA -= capacity;
                    day.setScheduleA(day.getScheduleA()+capacity);
                    day.setReservedA(day.getScheduleA());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        //******************************************Schedule for ProductA Completed**************************************************************
        for (DailyDetailEntity day:period){
            if (quantityB < (day.getScheduleB() - day.getReservedB())){
                day.setReservedB(day.getReservedB() + quantityB);
                quantityB = 0;
                ddsb.updateDailyDetail(day);
            }
            else {
                quantityB -= (day.getScheduleB() - day.getReservedB());
                day.setReservedB(day.getScheduleB());
                ddsb.updateDailyDetail(day);
            }
        }
        if (quantityB > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftCapacity()*50);
                if (quantityB < capacity){
                    day.setScheduleB(day.getScheduleB()+quantityB);
                    day.setReservedB(day.getScheduleB());
                    quantityB = 0;
                }
                else {
                    quantityB -= capacity;
                    day.setScheduleB(day.getScheduleB()+capacity);
                    day.setReservedB(day.getScheduleB());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        if (quantityB > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftOvertime()*50);
                if (quantityB < capacity){
                    day.setScheduleB(day.getScheduleB()+quantityB);
                    day.setReservedB(day.getScheduleB());
                    quantityB = 0;
                }
                else {
                    quantityB -= capacity;
                    day.setScheduleB(day.getScheduleB()+capacity);
                    day.setReservedB(day.getScheduleB());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        //******************************************Schedule for ProductB Completed**************************************************************
        for (DailyDetailEntity day:period){
            if (quantityC < (day.getScheduleC() - day.getReservedC())){
                day.setReservedC(day.getReservedC() + quantityC);
                quantityC = 0;
                ddsb.updateDailyDetail(day);
            }
            else {
                quantityC -= (day.getScheduleC() - day.getReservedC());
                day.setReservedC(day.getScheduleC());
                ddsb.updateDailyDetail(day);
            }
        }
        if (quantityC > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftCapacity()*45);
                if (quantityC < capacity){
                    day.setScheduleC(day.getScheduleC()+quantityC);
                    day.setReservedC(day.getScheduleC());
                    quantityC = 0;
                }
                else {
                    quantityC -= capacity;
                    day.setScheduleC(day.getScheduleC()+capacity);
                    day.setReservedC(day.getScheduleC());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        if (quantityC > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftOvertime()*45);
                if (quantityC < capacity){
                    day.setScheduleC(day.getScheduleC()+quantityC);
                    day.setReservedC(day.getScheduleC());
                    quantityC = 0;
                }
                else {
                    quantityC -= capacity;
                    day.setScheduleC(day.getScheduleC()+capacity);
                    day.setReservedC(day.getScheduleC());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        //******************************************Schedule for ProductC Completed**************************************************************
        for (DailyDetailEntity day:period){
            if (quantityD < (day.getScheduleD() - day.getReservedD())){
                day.setReservedD(day.getReservedD() + quantityD);
                quantityD = 0;
                ddsb.updateDailyDetail(day);
            }
            else {
                quantityD -= (day.getScheduleD() - day.getReservedD());
                day.setReservedD(day.getScheduleD());
                ddsb.updateDailyDetail(day);
            }
        }
        if (quantityD > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftCapacity()*45);
                if (quantityD < capacity){
                    day.setScheduleD(day.getScheduleD()+quantityD);
                    day.setReservedD(day.getScheduleD());
                    quantityD = 0;
                }
                else {
                    quantityD -= capacity;
                    day.setScheduleD(day.getScheduleD()+capacity);
                    day.setReservedD(day.getScheduleD());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        if (quantityD > 0){
            for (DailyDetailEntity day:period){
                int capacity = (int)(day.getLeftOvertime()*45);
                if (quantityD < capacity){
                    day.setScheduleD(day.getScheduleD()+quantityD);
                    day.setReservedD(day.getScheduleD());
                    quantityD = 0;
                }
                else {
                    quantityD -= capacity;
                    day.setScheduleD(day.getScheduleD()+capacity);
                    day.setReservedD(day.getScheduleD());
                    ddsb.updateDailyDetail(day);
                }
            }
        }
        //******************************************Schedule for ProductD Completed**************************************************************
        
        
    }
    
    
    public int getSmaller(int a, int b){
        if (a>b) return b;
        else return a;
    }
    
    public float getSmaller(float a, float b){
        if (a>b) return b;
        else return a;
    }
}
